import { TypeSwitch } from "../../type";
/**阴影算法 */
export function queryWidgetByShadowAlgorithm(widgetList: any[], direction: TypeSwitch.TypeDirection, widgetItem: TypeSwitch.WidgetItem) {
  const currentWidget = widgetItem.ele
  var currentRect = widgetItem.rect;
  // var resultWidget = null;
  // var resultRect = null;
  var resultItem = null;
  var dis = Infinity,
    tempDis;
  for (var i = 0; i < widgetList.length; i++) {
    // let _widgetId = widgetList[i].uid
    var tempWidget = widgetList[i].ele
    if (tempWidget === currentWidget) {
      continue;
    }
    var tempWidgetRect = widgetList[i].rect;
    if (tempWidgetRect.height == 0 || tempWidgetRect.width == 0) continue;
    if (
      direction === "LEFT" &&
      tempWidgetRect.right < currentRect.centerX &&
      tempWidgetRect.left < currentRect.left &&
      tempWidgetRect.top < currentRect.bottom &&
      tempWidgetRect.bottom > currentRect.top
    ) {
      tempDis = currentRect.right - tempWidgetRect.right;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerY - tempWidgetRect.centerY) <
          Math.abs(
            currentRect.centerY - widgetList[i].rect.centerY
          ))
      ) {
        // resultWidget = tempWidget;
        // resultRect = widgetList[i].rect;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "RIGHT" &&
      tempWidgetRect.left > currentRect.centerX &&
      tempWidgetRect.right > currentRect.right &&
      tempWidgetRect.top < currentRect.bottom &&
      tempWidgetRect.bottom > currentRect.top
    ) {
      tempDis = tempWidgetRect.left - currentRect.left;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerY - tempWidgetRect.centerY) <
          Math.abs(
            currentRect.centerY - widgetList[i].rect.centerY
          ))
      ) {
        // resultWidget = tempWidget;
        // resultRect = widgetList[i].rect;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "UP" &&
      tempWidgetRect.bottom < currentRect.centerY &&
      tempWidgetRect.top < currentRect.top &&
      tempWidgetRect.left < currentRect.right &&
      tempWidgetRect.right > currentRect.left
    ) {
      tempDis = currentRect.bottom - tempWidgetRect.bottom;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerX - tempWidgetRect.centerX) <
          Math.abs(
            currentRect.centerX - widgetList[i].rect.centerX
          ))
      ) {
        // resultWidget = tempWidget;
        // resultRect = widgetList[i].rect;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "DOWN" &&
      tempWidgetRect.top > currentRect.centerY &&
      tempWidgetRect.bottom > currentRect.bottom &&
      tempWidgetRect.left < currentRect.right &&
      tempWidgetRect.right > currentRect.left
    ) {
      tempDis = tempWidgetRect.top - currentRect.top;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerX - tempWidgetRect.centerX) <
          Math.abs(
            currentRect.centerX - widgetList[i].rect.centerX
          ))
      ) {
        // resultWidget = tempWidget;
        // resultRect = widgetList[i].rect;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    }
  }
  return resultItem
}


/**区域算法 */
export function queryWidgetByAreaAlgorithm(widgetList: any[], direction: TypeSwitch.TypeDirection, widgetItem: TypeSwitch.WidgetItem) {
  const currentWidget = widgetItem.ele
  var currentRect = widgetItem.rect;
  // var resultWidget = null;
  var resultItem = null;
  var dis = Infinity,
    tempDis;
  for (var i = 0; i < widgetList.length; i++) {
    // let _widgetId = widgetList[i].uid
    var tempWidget = widgetList[i].ele
    if (tempWidget === currentWidget) {
      continue;
    }
    var tempWidgetRect = widgetList[i].rect;
    if (tempWidgetRect.height == 0 || tempWidgetRect.width == 0) continue;

    if (direction === "LEFT" && tempWidgetRect.right <= currentRect.left) {
      tempDis = Infinity;
      if (tempWidgetRect.bottom < currentRect.top) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.left - tempWidgetRect.right, 2) +
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2)
        );
      } else if (tempWidgetRect.top > currentRect.bottom) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.left - tempWidgetRect.right, 2) +
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2)
        );
      } else {
        tempDis = currentRect.left - tempWidgetRect.right;
      }
      if (tempDis < dis) {
        // resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "RIGHT" &&
      tempWidgetRect.left >= currentRect.right
    ) {
      tempDis = Infinity;
      if (tempWidgetRect.bottom < currentRect.top) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.left - currentRect.right, 2) +
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2)
        );
      } else if (tempWidgetRect.top > currentRect.bottom) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.left - currentRect.right, 2) +
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2)
        );
      } else {
        tempDis = tempWidgetRect.left - currentRect.right;
      }
      if (tempDis < dis) {
        // resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (direction === "UP" && tempWidgetRect.bottom <= currentRect.top) {
      tempDis = Infinity;
      if (tempWidgetRect.right < currentRect.left) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2) +
          Math.pow(currentRect.left - tempWidgetRect.right, 2)
        );
      } else if (tempWidgetRect.left > currentRect.right) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2) +
          Math.pow(tempWidgetRect.left - currentRect.right, 2)
        );
      } else {
        tempDis = currentRect.top - tempWidgetRect.bottom;
      }
      if (tempDis < dis) {
        // resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (direction === "DOWN" && tempWidgetRect.top >= currentRect.bottom) {
      tempDis = Infinity;
      if (tempWidgetRect.right < currentRect.left) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2) +
          Math.pow(currentRect.left - tempWidgetRect.right, 2)
        );
      } else if (tempWidgetRect.left > currentRect.right) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2) +
          Math.pow(tempWidgetRect.left - currentRect.right, 2)
        );
      } else {
        tempDis = tempWidgetRect.top - currentRect.bottom;
      }
      if (tempDis < dis) {
        // resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    }
  }
  return resultItem
}